<template>
  <div class="container">
    <el-container class="contain">

      <div class="content">
        <div class="top">
          <div class="header">
            <!-- <img class="logo" src=“https://raw.githubusercontent.com/vueComponent/ant-design/master/logo.png” /> -->
            <span class="title">欢迎登录</span>
          </div>
          <div class="desc">这是一句系统说明
            <lang-select class="set-language"></lang-select>
          </div>

        </div>
        <div class="login-container">
          <el-form class="login-form" autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left">

            <el-form-item prop="username">
              <el-input name="username" type="text" v-model="loginForm.username" autoComplete="on" placeholder="请输入用户名">
                <template slot="prefix">
                  <svg-icon icon-class="user" class="svg-icon-prefix" />
                </template>
              </el-input>
            </el-form-item>

            <el-form-item prop="password">
              <el-input name="password" :type="passwordType" @keyup.enter.native="handleLogin" v-model="loginForm.password" autoComplete="on" placeholder="请输入密码">
                <template slot="prefix">
                  <svg-icon icon-class="password" class="svg-icon-prefix" />
                </template>
                <template slot="suffix">
                  <span class="show-pwd" @click="showPwd">
                    <svg-icon icon-class="eye" />
                  </span>
                </template>
              </el-input>
            </el-form-item>

            <el-form-item prop="rememberMe">
              <el-checkbox :label="$t('login.rememberMe')" name="rememberMe" v-model="loginForm.rememberMe"></el-checkbox>
            </el-form-item>
            <el-button type="primary" style="width:100%;margin-bottom:30px;" :loading="loading" @click.native.prevent="handleLogin">{{$t('login.logIn')}}</el-button>
            <el-button v-if="false" class="thirdparty-button" type="primary" @click="showDialog=true">{{$t('login.thirdparty')}}</el-button>
          </el-form>
        </div>
      </div>

      <el-footer>
        <div class="copyright">Copyright
          <svg-icon icon-class="copyright" @click="showPwd" /> 2018 XX公司</div>
      </el-footer>

    </el-container>
  </div>
</template>


<script>
import { isvalidUsername } from '@/utils/validate'
import LangSelect from '@/components/LangSelect'
import SocialSign from './socialsignin'

export default {
  components: { LangSelect, SocialSign },
  name: 'login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: '',
        rememberMe: false
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'password',
      loading: false,
      showDialog: false
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('LoginByUsername', this.loginForm).then(() => {
            this.loading = false
            this.$router.push({ path: '/' })
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    },
    afterQRScan() {
      // const hash = window.location.hash.slice(1)
      // const hashObj = getQueryObject(hash)
      // const originUrl = window.location.origin
      // history.replaceState({}, '', originUrl)
      // const codeMap = {
      //   wechat: 'code',
      //   tencent: 'code'
      // }
      // const codeName = hashObj[codeMap[this.auth_type]]
      // if (!codeName) {
      //   alert('第三方登录失败')
      // } else {
      //   this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
      //     this.$router.push({ path: '/' })
      //   })
      // }
    }
  },
  created() {
    // window.addEventListener('hashchange', this.afterQRScan)
  },
  destroyed() {
    // window.removeEventListener('hashchange', this.afterQRScan)
  }
}
</script>


<style rel="stylesheet/scss" lang="scss" scoped>
$screen-md-min: 600px;
$heading-color: black;
$font-size-base: 14px;
$text-color-secondary: rgba(0, 0, 0, 0.45);

.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  .login-form {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    padding: 35px 35px 15px 35px;
    margin: 0px auto;
    .svg-icon-prefix {
      margin-left: 5px;
    }
    .showPwd {
      margin-right: 10px;
    }
  }
}

.container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: auto;
  background: #f0f2f5;
}

.content {
  padding: 32px 0;
  flex: 1;
}

.bg-purple {
  background: #d3dce6;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

@media (min-width: $screen-md-min) {
  .container {
    background-image: url("https://gw.alipayobjects.com/zos/rmsportal/TVYTbAXWheQpRcWDaDMu.svg");
    background-repeat: no-repeat;
    background-position: center 110px;
    background-size: 100%;
  }

  .content {
    padding: 85px 0 24px 0;
  }
}

.top {
  text-align: center;
}

.header {
  height: 44px;
  line-height: 44px;
  a {
    text-decoration: none;
  }
}

.logo {
  height: 44px;
  vertical-align: top;
  margin-right: 16px;
}

.title {
  font-size: 33px;
  color: $heading-color;
  font-family: "Myriad Pro", "Helvetica Neue", Arial, Helvetica, sans-serif;
  font-weight: 600;
  position: relative;
  top: 2px;
}

.desc {
  font-size: $font-size-base;
  color: $text-color-secondary;
  margin-top: 12px;
  margin-bottom: 30px;
}

.copyright {
  text-align: center;
  color: $text-color-secondary;
}
</style>
